package com.yonyou.pmclouds.enterprise.controller;

import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/rest/enterprise")
public class EnterpriseOutController {
    @Autowired
    private EnterpriseInfoQuery infoQuery;

    @GetMapping("/search")
    public EnterpriseInfo[] search(@RequestParam("keyword") String keyword,
                                   @RequestParam(value = "maxNum", defaultValue = "10", required = false) String maxNum) {
        EnterpriseInfo[] infos = infoQuery.searchEnterprise(keyword);
        if (infos == null) {
            infos = new EnterpriseInfo[0];
        }
        int length = Math.min(Integer.valueOf(maxNum), infos.length);
        return ArrayUtils.subarray(infos, 0, length);
    }
}
